/*****************************************************************************
 * naite.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "thirdparty/modbus/modbus.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "dev.motor_valve"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define DEV_VALVE_REG_STA  0
#define DEV_VALVE_REG_CTRL 1
/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_u16 addr;
    plum_u16 value;
} motor_valve_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE motor_valve_t reg_tb[] = {
    [DEV_VALVE_REG_STA]  = {0x0001, 0},  //开合度状态
    [DEV_VALVE_REG_CTRL] = {0x0002, 0},  //写开合度
};
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void dev_motor_valve_notice(modbus_protocol_evt evt, plum_void *arg)
{
    switch (evt) {
        case MODBUS_EVT_MSG:
        {
            modbus_msg_t *msg = (modbus_msg_t *)arg;
            plum_u8       loop;
            for (loop = 0; loop < PLUM_ARRAY_CNT(reg_tb); loop++) {
                if (reg_tb[loop].addr == msg->addr) {
                    reg_tb[loop].value = *(plum_u16 *)msg->value;
                    break;
                }
            }
        } break;

        case MODBUS_EVT_ERR:
        {
            modbus_err_t *msg = (modbus_err_t *)arg;
            LOG_E("modbus err:%s", msg->discr);
        } break;

        default:
            LOG_E("dev_vfd_c10_notice err evt");
            break;
    }
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

plum_s32 dev_proportional_valve_write(plum_u8 id, plum_u8 per)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        plum_u16 value = per * 10 + 1999;

        rc = modbus_protocol_write(MODBUS_PROTOCOL_RTU, id,
                                   MODBUS_PROTOCOL_WRITE_SHOLD,
                                   reg_tb[DEV_VALVE_REG_CTRL].addr, &value, 2);
        if (rc) {
            LOG_E("modbus_protocol_write err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

plum_s32 dev_proportional_valve_read(plum_u8 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        rc = modbus_protocol_register_notice(dev_motor_valve_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  reg_tb[DEV_VALVE_REG_STA].addr, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc: %d", rc);
            break;
        }
    } while (0);

    rc = reg_tb[DEV_VALVE_REG_STA].value
             ? (reg_tb[DEV_VALVE_REG_STA].value - 1999) / 10
             : 0;

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
